//
//  RecommendViewController.swift
//  DouYuZB
//
//  Created by 梁森 on 2020/4/21.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

private let kItemPrettyHeight:CGFloat = itemWidth * 4 / 3
private let kCycleViewH:CGFloat = kScreenWidth * 3 / 8
private let kGameViewH:CGFloat = 90

class RecommendViewController: BaseAnchorViewController {

    // MARK: 懒加载

    /// 轮播图
    lazy var cycleView:RecommendCycleView = {
        let cycleView = RecommendCycleView.cycleView()
        cycleView.frame = CGRect(x: 0, y: -(kCycleViewH + kGameViewH), width: kScreenWidth, height: kCycleViewH)
        return cycleView
    }()
    /// 游戏推荐视图
    lazy var gameView:RecommendGameView = {
        let gameView = RecommendGameView()
        gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenWidth, height: kGameViewH)
        return gameView
    }()
    
    private let recViewModel:RecommendViewModel = RecommendViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
      print("RecommendViewController😄😄😄😄");
    }
}
// MARK: 遵守协议
extension RecommendViewController{
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let section = indexPath.section
        let room_list = recViewModel.anchorGroups[section].room_list
        var cell:BaseHomeCollectionViewCell = BaseHomeCollectionViewCell()
        if section == 1{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: kItemPrettyId, for: indexPath) as! CollectionPrettyCell
        }else{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: itemId, for: indexPath) as! CollectionNormalCell
        }
        cell.room = room_list[indexPath.row]
        cell.backgroundColor = UIColor.white
        return cell
    }
    override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let section = indexPath.section
        if section == 1 {
            return CGSize(width: itemWidth, height: kItemPrettyHeight)
        }else{
            return CGSize(width: itemWidth, height: itemHeight)
        }
    }
}
// MARK: 设置UI
extension RecommendViewController{
    override func setupUI(){
        super.setupUI()
        collectionView.contentInset = UIEdgeInsets(top: (kCycleViewH + kGameViewH), left: 0, bottom: 0, right: 0)
        collectionView.addSubview(cycleView)
        collectionView.addSubview(gameView)
    }
}
// MARK: 网络数据
extension RecommendViewController{
    override func getData(){
        baseVM = recViewModel
        recViewModel.requestData {
            self.collectionView.reloadData()
            var groups = self.recViewModel.anchorGroups
            groups.removeFirst()
            groups.removeFirst()
            let moreAnchor = AnchorGroup()
            moreAnchor.tag_name = "更多"
            groups.append(moreAnchor)
            self.gameView.groups = groups
            
            self.requestDataFinish()
        }
        self.recViewModel.requestCycleData(finishCallBack: {
            self.cycleView.cycleDatas = self.recViewModel.cycleDatas
        }) {
            
        }
    }
}
